<template>
  <div class="app-container">
    <div class="tabletitle">
      <div>便签</div>
      <div>
        <el-button type="primary" @click="addnote">
          <i class="el-icon-plus" />&nbsp;添加
        </el-button>
        <span style="cursor: pointer;margin-left: 10px;  font-weight: 600;">
          <i class="iconfont iconhelp" @click="preview" style="font-size:18px"></i>
        </span>
      </div>
    </div>
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      style="margin-top:20px;  border-bottom: 1px solid #dfe6ec;"
    >
      <el-form-item label="便签内容" prop="name">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入便签内容"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="关联项目" prop="projectId">
        <el-select
          filterable
          clearable
          v-model="queryParams.projectId"
          placeholder="请选择关联项目"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in projectList"
            :key="dict.guid"
            :label="dict.name"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联业务" prop="businessId">
        <el-select
          filterable
          clearable
          v-model="queryParams.businessId"
          placeholder="请选择关联业务"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in businessList"
            :key="dict.guid"
            :label="dict.name"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联干系人" prop="stakeholderId" label-width="90px">
        <el-select
          filterable
          clearable
          v-model="stakeholderId"
          placeholder="请选择关联干系人"
          size="small"
          style="width: 240px"
          :multiple="true"
        >
          <el-option
            v-for="dict in stakeholderList"
            :key="dict.guid"
            :label="dict.name"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      stripe
      v-loading="loading"
      :data="tableData"
      style="width:100%;"
      @sort-change="tableSortChange"
      :default-sort="{prop: 'createTime', order: 'descending'}"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column
        prop="content"
        :show-overflow-tooltip="true"
        label="便签内容"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        prop="projectName"
        label="关联项目"
        :show-overflow-tooltip="true"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        prop="businessName"
        label="关联业务"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column label="关联干系人" align="center" width="200">
        <template slot-scope="scope">
          <span>{{ getStakeholderName(scope.row.stakeholderList) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        width="200"
        label="创建时间"
        align="center"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column width="140" label="操作" align="center">
        <template slot-scope="scope">
          <a @click="handleEdit(scope.$index, scope.row)">
            <i class="el-icon-edit aicon" />编辑
          </a>
          <a @click="handleDelete(scope.$index, scope.row)">
            <i class="el-icon-delete aicon" />删除
          </a>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <EditNote
      :dialog-visible="dialogVisible"
      :note-id="noteId"
      :is-edit="isEdit"
      v-if="dialogVisible"
      @changedialogVisible="changedialogVisible"
    />
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import EditNote from '@/components/Note/editNote';
import {
  getProjectList,
  getBusinessList,
  getStakeholderList,
  getNoteList,
  delNote
} from '@/api/note';
export default {
  name: 'NoteList',
  components: {
    EditNote
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      isEdit: false,
      total: 0,
      noteId: null,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        projectId: undefined,
        businessId: undefined,
        stakeholderId: undefined
      },
      stakeholderId: undefined,
      loading: true,
      projectList: [],
      businessList: [],
      stakeholderList: [],
      dialogVisible: false,
      tableData: []
    }
  },
  async created () {
    const loading = this.$loading({
      lock: true,
      text: 'Loading'
    })
    await getProjectList({
      userGuid: JSON.parse(sessionStorage.getItem('userinfo') || {}).userId
    }).then(res => {
      this.projectList = res.data
    })
    await getBusinessList().then(res => {
      this.businessList = res.data
    })
    await getStakeholderList().then(res => {
      this.stakeholderList = res.data
      loading.close()
    })
    this.getList()
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}首页/便签.xlsx`
      )}&name=${encodeURIComponent("便签.xlsx")}`;
    },
    getList () {
      this.loading = true
      this.queryParams.createBy = JSON.parse(
        sessionStorage.getItem('userinfo') || {}
      ).userId
      this.queryParams.stakeholderId =
        this.stakeholderId === undefined
          ? undefined
          : this.stakeholderId.join(',')
      getNoteList(this.queryParams).then(res => {
        if (res.code === 200) {
          this.tableData = res.rows
          this.loading = false
          this.total = res.total
        }
      })
    },
    addnote () {
      this.dialogVisible = true
      this.noteId = null
      this.isEdit = false
      // this.$router.push('/noteList/editInfo')
    },
    handleEdit (index, row) {
      this.noteId = row.guid
      this.isEdit = true
      this.dialogVisible = true

      console.log(index, row)
    },
    handleDelete (index, row) {
      this.$confirm('是否确认删除便签?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return delNote(row.guid)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => { })
      console.log(index, row)
    },
    changedialogVisible (val) {
      console.log(val)
      this.getList()
      this.dialogVisible = val
    },
    reset () {
      this.queryParams = {
        pageNum: 1,
        projectId: undefined,
        businessId: undefined,
        stakeholderId: undefined
      }
      this.stakeholderId = undefined
      this.getList()
    },
    getStakeholderName (rows) {
      let getStakeholderName = '';
      rows.map((item, index) => {
        if (index === rows.length - 1) {
          getStakeholderName += item.stakeholderName || '';
        } else {
          getStakeholderName += item.stakeholderName + ',';
        }
      })
      return getStakeholderName
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
}
</script>
<style lang="scss" scoped>
.tabletitle {
  padding-bottom: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #dfe6ec;
  span {
    font-family: Microsoft YaHei;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    letter-spacing: -0.41px;
  }
  .el-button--medium {
    font-size: 12px;
    padding: 7px 13px;
  }
}

/deep/.el-table .cell {
  font-family: PingFangSC-Regular;
  color: rgba(0, 0, 0, 0.85);
  letter-spacing: 0;
  a {
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #1890ff;
    letter-spacing: 0;
    text-align: right;
    line-height: 14px;
    margin-left: 5px;
  }
}

/deep/.el-table--medium td {
  padding: 12px 0;
}
</style>
